import { AdManager } from "../ads/AdManager";
import { AdManager_ZJ } from "../ads/AdManager_ZJ";
import { Tools } from "../common/Tools";

/** 网络相关 管理类 */
export class NetworkManager {
    /** 初始化 网络相关 */
    public static initNetwork() {

    }
    /** 向巨量引擎服务器  发送 加载游戏成功 */
    public static send_JuLiang(eventStr: string = "active", clickID?: string) {
        return;
        if (!clickID) {
            cc.log("clickID 为 null");
            return;
        }
        if (eventStr == "active") {
            this.sendMsg("https://analytics.oceanengine.com/api/v2/conversion", {
                "event_type": eventStr,
                "context": {
                    "ad": {
                        "callback": clickID,//callback 这里需要填写的就是从启动参数里获取的 clickid
                    }
                },
                "timestamp": new Date().getTime()
            }, () => {
                Tools.setStorage("juLiangActive", 1);
            });
        } else {
            this.sendMsg("https://analytics.oceanengine.com/api/v2/conversion", {
                "event_type": eventStr,
                "context": {
                    "ad": {
                        "callback": clickID,//callback 这里需要填写的就是从启动参数里获取的 clickid
                    }
                },
                "timestamp": new Date().getTime()
            }
                , this.send_JuLiangBack);
        }
    };
    /** 向服务器 提交分数回调  */
    private static send_JuLiangBack(backData: any) {
        cc.log("send_JuLiangBack:", backData);
    }

    /**获取用户openid接口 */
    public static getOpenId(gameId: number, gameType: number, code: string, callback?: any) {
        if (!code) return;
        this.sendMsg("https://main.shuling.vip/api/game/getopenid", {
            "gameId": gameId,
            "gameType": gameType,
            "code": code
        }, (res) => {
            if (res == null) {
                console.log("获取json配置异常", res);
                callback(null);
                return;
            }
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                // AdManager_ZJ.ins.openId = ret.data;
                AdManager.openId = ret.data;
                if (callback) {
                    console.log("进行开始时间数据上报", AdManager.openId);
                    callback(ret.data);
                }
            }
        })
    }

    /**上报用户游戏时长接口 
     * @param vtype vtype=1开始时间 vtype=2结束时间
    */
    public static reportGameTime(gamesId: number, openId: string, vtype: number, clickid: string = null, gamesType: number,projectId:string,advertiser_id:number) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/durationStart", {
            "gamesId": gamesId,
            "openId": openId,
            "vtype": vtype,
            "clickid": clickid,
            "gamesType": gamesType,
            "projectId":projectId,
            "advertiser_id":advertiser_id
        }, (res) => {
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("上报游戏时长正常类型为：", vtype);
            } else {
                console.log("上报游戏时长异常", res);
            }
        })
    }

    /**视频点击接口 
     * @param vtype 1提示 2答案 3解锁关卡 4加时 5跳过本关 6加步数
     * @param gamesType 1微信 2抖音
     * @param clickid 抖音点击广告进入游戏的才有
     * @param cliKey 抖音点击广告进入游戏的才有
     * @param levelId 关卡顺序
     * @param levelName 关卡名称
    */
    public static reportVideo(gamesId: number, openId: string, vtype: number, gamesType: number, levelId: number, levelName: string, callback: any, clickid: string = null, cliKey: string = null) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/videopoints", {
            "gamesId": gamesId,
            "openId": openId,
            "vtype": vtype,
            "gamesType": gamesType,
            "cliKey": cliKey,
            "levelId": levelId,
            "levelName": levelName
        }, (res) => {
            if (res == null) {
                console.log("获取json配置异常", res);
                callback(null);
                return;
            }
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("视频上报正常");
                callback(ret.data);
            } else {
                console.log("视频上报异常", res);
            }
        })
    }

    /**视频结束触发接口
     * @param levelKey 即视频上报接口中返回的data数据
     * @param levelId 关卡顺序
     * @param levelName 关卡名称
     */
    public static reportOverVideo(gamesId: number, openId: string, levelKey: string, levelId: number, levelName: string, clickid: string = null, projectId: string = null, promotionId: string = null,
        advertiser_id: number = null) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/overVideo", {
            "gamesId": gamesId,
            "openId": openId,
            "levelKey": levelKey,
            "levelId": levelId,
            "levelName": levelName,
            "clickid": clickid,
            "projectId": projectId,
            "promotionId": promotionId,
            "advertiser_id": advertiser_id
        }, (res) => {
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("视频结束上报正常");
            } else {
                console.log("视频结束上报异常", res);
            }
        })
    }

    /**用户关卡接口
     * @param levelId 关卡顺序
     * @param levelName 关卡名称
     * @param isOver 进入此关卡：0 通关此关卡：1
     */
    public static reportLevel(gamesId: number, openId: string, levelId: number, levelName: string, isOver: number) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/userLocation", {
            "gamesId": gamesId,
            "openId": openId,
            "levelId": levelId,
            "levelName": levelName,
            "isOver": isOver
        }, (res) => {
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("关卡上报正常");
            } else {
                console.log("关卡上报异常", res);
            }
        })
    }

    /**监听用户回到首页 */
    public static reportHomeView(gamesId: number, openId: string) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/goHome", {
            "gamesId": gamesId,
            "openId": openId,
        }, (res) => {
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("返回首页上报正常");
            } else {
                console.log("返回首页上报异常", res);
            }
        })
    }

    /**获取游戏关卡json配置 */
    public static getConfigById(gamesId: number, callback) {
        console.log("xxxxxxxxxxxxxxxxxxxxxxgameID:", gamesId);
        this.sendMsg("https://main.shuling.vip/api/game/getConfigById", {
            "gameId": gamesId,
            "type": 1,
        }, (res) => {
            if (res == null) {
                console.log("获取json配置异常", res);
                callback(null);
                return;
            }
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("获取json配置正常");
                let lvJson = JSON.parse(ret.data);
                callback(lvJson);
            } else {
                console.log("获取json配置异常", res);
                callback(null);
            }
        })
    }

    /**上报用户退出本关接口 */
    public static exitLevel(gamesId: number, openId: string, levelName: string) {
        if (!openId) return;
        this.sendMsg("https://main.shuling.vip/api/game/ExitLevel", {
            "gamesId": gamesId,
            "openId": openId,
            "levelName": levelName,
        }, (res) => {
            let ret = JSON.parse(res);
            if (ret.code == 1) {
                console.log("退出关卡上报正常");
            } else {
                console.log("退出关卡上报异常", res);
            }
        })
    }

    /**测试接口 */
    public static serverTest() {
        // this.sendMsg("https://www.ipplus360.com/getLocation", {
        // }, (res) => {
        //     if (res == null) {
        //         console.log("请求异常", res);
        //     }
        //     let ret = JSON.parse(res);
        //     if (ret.code == 1) {
        //         console.log("请求成功");
        //     } else {
        //         console.log("请求异常2retCode!=1");
        //     }
        // })
    }

    /** 向服务器 POST请求 http */
    public static sendMsg(url: string, postData: any, callback?: any) {
        cc.log("sendMsg:", postData);
        var request = cc.loader.getXMLHttpRequest();
        request.timeout = 2000;
        request.onreadystatechange = (ev: Event) => {
            if (request.readyState == 4 && request.status == 200) {
                callback(request.responseText);
            }
        };
        request.ontimeout = function (event) {
            console.log("请求超时");
            callback(null);
        }
        request.open("POST", url);
        request.setRequestHeader("Content-Type", "application/json");
        request.send(postData);
    };
}